package com.yjd.pub.base.service;

import com.yjd.comm.base.dao.IBaseDao;
import com.yjd.comm.base.model.MfqTokenModel;
import com.yjd.comm.base.service.IMfqTokenService;
import com.yjd.comm.base.service.impl.BaseServiceImpl;
import com.yjd.comm.util.FrameUtil;
import com.yjd.comm.util.StringUtil;
import com.yjd.pub.base.dao.IMfqTokenDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <b>description</b>：系统token表业务实现<br>
 * <b>time</b>：2017-06-29 10:36:22 <br>
 * <b>author</b>：  carl
 */
@Service("mfqTokenService")
public class MfqTokenServiceImpl extends BaseServiceImpl implements IMfqTokenService {

	/**
	 * 插入
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public MfqTokenModel insert(MfqTokenModel model) throws Exception {
		return this.mfqTokenDao.insertModel(model, true);
	}

	/**
	 * 更新
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public int update(MfqTokenModel model) throws Exception {
		return this.mfqTokenDao.updateModel(model, true);
	}

	/**
	 * 根据id删除
	 * 
	 * @param token_key
	 * @return
	 * @throws Exception
	 */
	public int delete(String token_key) throws Exception {
		Map<Object, Object> paramMap = new HashMap<Object, Object>();
		paramMap.put("token_key", token_key);
		return this.mfqTokenDao.deleteModel(paramMap, true);
	}

    /**
     * 根据id删除,大于等于1表示成功，其他失败
     *
     * @param token_key
     * @return
     * @throws Exception
     */
    @Override
    public int delete(long token_key) throws Exception {
        Map<Object, Object> paramMap = new HashMap<Object, Object>();
        paramMap.put("token_key", token_key);
        return this.mfqTokenDao.deleteModel(paramMap, true);
    }

    /**
	 * 存放一个token
	 *
	 * @param token_key
	 * @param token_value
	 * @throws Exception
	 */
	@Override
	public void pushKey(String token_key, String token_value) throws Exception {
		if(StringUtil.isEmpty(token_key)||StringUtil.isEmpty(token_value)){
			FrameUtil.throwBaseException1("请求参数不能为空");
		}
		MfqTokenModel tokenModel = this.mfqTokenDao.getModelById(token_key,true);
		if(tokenModel!=null){
			tokenModel.setToken_key(token_key);
			tokenModel.setToken_value(token_value);
			tokenModel.setAddtime(FrameUtil.getTime(null));
			this.mfqTokenDao.updateModel(tokenModel,true);
		}else{
			tokenModel = new MfqTokenModel();
			tokenModel.setToken_key(token_key);
			tokenModel.setToken_value(token_value);
			tokenModel.setAddtime(FrameUtil.getTime(null));
			this.mfqTokenDao.insertModel(tokenModel,true);
		}

	}

	/**
	 * 获取token的值
	 *
	 * @param token_key
	 * @throws Exception
	 */
	@Override
	public boolean valDateToken(String token_key,String token_value) throws Exception {
		boolean flag =false;
		if(StringUtil.isEmpty(token_key)||StringUtil.isEmpty(token_value)){
			FrameUtil.throwBaseException1("请求参数不能为空");
		}

		MfqTokenModel tokenModel = this.mfqTokenDao.getModelById(token_key,true);
		if(tokenModel==null){
			return flag;
		}
		if(token_value.equals(tokenModel.getToken_value())){
			this.mfqTokenDao.deleteById(token_key,true);
			flag = true;
		}
		return flag;
	}

	@Resource
	private IMfqTokenDao mfqTokenDao;

	@Override
	public IBaseDao getBaseDao() {
		return this.mfqTokenDao;
	}

}
